
function Game(ele) {
    // 地图
    this.ele = document.querySelector(ele)
    this.map = new Map(this.ele)//将map这个地址存进el中
    this.map.create()
    // 食物
    this.food = new Food(this.ele);
    this.food.create()
    // 蛇
    this.snake = new Snake(this.ele)
    this.snake.create()
}

Game.prototype.start = function () {
    setInterval(() => {
        this.snake.move()
        this.snake.die()
        let snakeX = this.snake.Snakeobj[0].x * 20
        let snakeY = this.snake.Snakeobj[0].y * 20
        let foodX = this.food.foodX
        let foodY = this.food.foodY
        if (snakeX == foodX && snakeY == foodY) {
            let last = this.snake.Snakeobj[this.snake.Snakeobj.length - 1]


            this.snake.Snakeobj.push({
                x: last.x,
                y: last.y,
                color: last.color,
                ele: null
            })
            this.snake.create()
            this.food.move()
        }
        if (snakeX < 0 || snakeX >= 800) alert('你有瑕疵啊凸皿凸')
        if (snakeY < 0 || snakeY >= 400) alert('你有瑕疵啊凸皿凸')
    }, 200)

    document.addEventListener('keydown', (e) => {
        e = e || window.event
        switch (e.keyCode) {
            case 38:
                if (this.snake.direction == 'bottom') {
                    alert('游戏结束')
                }
                this.snake.direction = 'top'
                break
            case 40:
                if (this.snake.direction == 'top') {
                    alert('游戏结束')
                }
                this.snake.direction = 'bottom'
                break
            case 37:
                if (this.snake.direction == 'right') {
                    alert('游戏结束')
                }
                this.snake.direction = 'left'
                break
            case 39:
                if (this.snake.direction == 'left') {
                    alert('游戏结束')
                }
                this.snake.direction = 'right'
                break

        }
    })
}